package cn.iocoder.yudao.module.energy.service.statistics.impl;

import cn.iocoder.yudao.module.energy.controller.admin.statistics.vo.StatisticsRespVO;
import cn.iocoder.yudao.module.energy.service.alert.AlertService;
import cn.iocoder.yudao.module.energy.service.device.DeviceService;
import cn.iocoder.yudao.module.energy.service.statistics.StatisticsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 能源统计 Service 实现类
 */
@Service
public class StatisticsServiceImpl implements StatisticsService {

    @Resource
    private DeviceService deviceService;

    @Resource
    private AlertService alertService;

    @Override
    public StatisticsRespVO getStatistics() {
        // 创建返回结果
        StatisticsRespVO respVO = new StatisticsRespVO();
        
        // 1. 设备总数
        respVO.setDeviceCount(deviceService.getDeviceCount());
        
        // 2. 设备能耗总数
        respVO.setTotalEnergy(deviceService.getTotalEnergy());
        
        // 3. 今日预警数量
        LocalDateTime startTime = LocalDateTime.now().withHour(0).withMinute(0).withSecond(0);
        LocalDateTime endTime = LocalDateTime.now();
        respVO.setTodayAlertCount(alertService.getAlertCount(startTime, endTime));
        
        // 4. 总预警数量
        respVO.setTotalAlertCount(alertService.getTotalAlertCount());
        
        // 5. 近7天每天各类型预警数量统计
        LocalDateTime sevenDaysAgo = LocalDateTime.now().minusDays(7).withHour(0).withMinute(0).withSecond(0);
        List<StatisticsRespVO.DailyAlertTypeCountVO> dailyAlertTypeCounts = alertService.getDailyAlertTypeCount(sevenDaysAgo, LocalDateTime.now());
        respVO.setDailyAlertTypeCounts(dailyAlertTypeCounts);
        
        // 6. 近30天每天各类型预警数量统计
        LocalDateTime thirtyDaysAgo = LocalDateTime.now().minusDays(30).withHour(0).withMinute(0).withSecond(0);
        List<StatisticsRespVO.DailyAlertTypeCountVO> monthlyDailyAlertTypeCounts = alertService.getDailyAlertTypeCount(thirtyDaysAgo, LocalDateTime.now());
        respVO.setMonthlyDailyAlertTypeCounts(monthlyDailyAlertTypeCounts);
        
        // 7. 近一年每月各类型预警数量统计（每月汇总一条数据，共12个月）
        LocalDateTime oneYearAgo = LocalDateTime.now().minusYears(1).withDayOfMonth(1).withHour(0).withMinute(0).withSecond(0);
        List<StatisticsRespVO.DailyAlertTypeCountVO> yearlyMonthlyAlertTypeCounts = alertService.getMonthlyAlertTypeCount(oneYearAgo, LocalDateTime.now());
        respVO.setYearlyMonthlyAlertTypeCounts(yearlyMonthlyAlertTypeCounts);
        
        // 8. 近7天按告警级别和预警类型分组的预警数量统计
        List<StatisticsRespVO.AlertLevelTypeCountVO> dailyAlertLevelTypeCounts = alertService.getDailyAlertLevelTypeCount(sevenDaysAgo, LocalDateTime.now());
        respVO.setDailyAlertLevelTypeCounts(dailyAlertLevelTypeCounts);
        
        // 9. 近30天按告警级别和预警类型分组的预警数量统计
        List<StatisticsRespVO.AlertLevelTypeCountVO> monthlyAlertLevelTypeCounts = alertService.getMonthlyAlertLevelTypeCount(thirtyDaysAgo, LocalDateTime.now());
        respVO.setMonthlyAlertLevelTypeCounts(monthlyAlertLevelTypeCounts);
        
        // 10. 近一年按告警级别和预警类型分组的预警数量统计
        List<StatisticsRespVO.AlertLevelTypeCountVO> yearlyAlertLevelTypeCounts = alertService.getYearlyAlertLevelTypeCount(oneYearAgo, LocalDateTime.now());
        respVO.setYearlyAlertLevelTypeCounts(yearlyAlertLevelTypeCounts);
        
        // 11. 按告警级别分组的全部预警数量统计
        Map<String, Long> alertLevelCounts = alertService.getAllAlertLevelCount();
        respVO.setAlertLevelCounts(alertLevelCounts);
        
        // 12. 按告警级别和预警类型分组的全部预警数量统计
        Map<String, Map<String, Long>> alertLevelTypeCounts = alertService.getAllAlertLevelTypeCount();
        respVO.setAlertLevelTypeCounts(alertLevelTypeCounts);
        
        // 13. 告警级别为warning的各类型预警数量统计
        Map<String, Long> alertWarningLevelCounts = alertService.getWarningLevelAlertCount();
        respVO.setAlertWarningLevelCounts(alertWarningLevelCounts);
        
        // 14. 告警级别为danger的各类型预警数量统计
        Map<String, Long> alertDangerLevelCounts = alertService.getDangerLevelAlertCount();
        respVO.setAlertDangerLevelCounts(alertDangerLevelCounts);
        
        return respVO;
    }
} 